package com.sk.bilibili.view.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;

import com.bumptech.glide.Glide;
import com.google.zxing.Result;
import com.google.zxing.client.result.ParsedResult;
import com.mylhyl.zxing.scanner.OnScannerCompletionListener;
import com.mylhyl.zxing.scanner.ScannerOptions;
import com.sk.bilibili.R;
import com.sk.bilibili.config.GlideApp;
import com.sk.bilibili.databinding.ActivityScanBinding;
import com.sk.bilibili.model.activity.ScanModel;
import com.sk.bilibili.utils.DataUtils;

public class ScanActivity extends BaseActivity<ActivityScanBinding, ScanModel> implements OnScannerCompletionListener {

    @Override
    protected void afterSetView() {
        initView();
        bindEvent();
    }

    private void bindEvent() {
        bind.ivBack.setOnClickListener(v->finish());
    }

    private void initView() {
        bind.svShow.setOnScannerCompletionListener(this);
        bind.svShow.setScannerOptions(model.getOptions());
        Integer memberId = DataUtils.getMemberId();
        GlideApp.with(this).load(model.generateUrl(memberId)).code().into(bind.ivCode);
        bind.tvUid.setText(String.format("我的UID：%d",memberId));
    }

    @Override
    protected void onPause() {
        super.onPause();
        bind.svShow.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        bind.svShow.onResume();
    }

    @Override
    public void onScannerCompletion(Result rawResult, ParsedResult parsedResult, Bitmap barcode) {
        setResult(RESULT_OK,model.handleRes(rawResult));
        finish();
    }

}